

local numSessions = 19

******* Bring qualtrics data into stata format ************************* 
if 1 {


	*** batch 1 ***

	insheet using "data/raw/Session1.csv", names
	gen id = _n
	
	replace id = 1000 + id

	destring, replace

	forvalues i = 1/18 {
		rename lm`i'_bet bet`i'
		rename lm`i'_plikert plik`i'
		rename lm`i'_q276_page tpicQualtrics`i'
		rename lm`i'_tbeliefs_pagesubmit tBeliefs`i'
		
		replace plik`i' = 1 if plik`i' == 29
		replace plik`i'= 0 if plik`i' == 16
	}
	
	
	forvalues i = 1 / 4 {
		local iPlus = `i' + 1
		forvalues j = 1 / 23 {
			rename lm`iPlus'_wta_wta_`j' lm`i'_wta_wta_`j'
		}
	}
	
		 
	gen orderriskSum = 0
	forvalues i = 1 / 8 {
		replace orderriskSum = orderriskSum + orderrisk`i'
	}
	gen orderrisk9 = 45 - orderriskSum
	 

	rename q280 major
	drop q* v*
	
	gen session = 1
	
	save "data/rawLab_1.dta", replace
	
	clear
	
	*** batch 2 to 19 ***

	forvalues session = 2 / `numSessions' {
	

		insheet using "data/raw/Session`session'.csv", names
		gen id = _n
		
		replace id = `session'*1000 + id

		destring, replace	

	
		forvalues i = 1/18 {
			rename lm`i'_bet bet`i'
			rename lm`i'_plikert plik`i'
			rename lm`i'_q276_page tpicQualtrics`i'
			rename lm`i'_tbeliefs_pagesubmit tBeliefs`i'
			
			replace plik`i' = 1 if plik`i' == 29
			replace plik`i'= 0 if plik`i' == 16
		}		
		
		rename q280 major

		drop q* v*
		
		gen session = `session'
		
		save "data/rawLab_`session'.dta", replace
		
		clear
	}
	
}



	



******* variable naming, reshaping etc. ******************************** 


	use "data/rawLab_1.dta", replace
	
	forvalues session = 2 / `numSessions' {
		append using "data/rawLab_`session'.dta", force
	}
	
	drop wtavector
	
	
	// Decision time
	if 1 {
		rename startdate startDate
		rename enddate endDate
		gen double startTime = clock(startDate, "MD20Yhm")
		gen double endTime = clock(endDate, "MD20Yhm")
		gen double duration = endTime - startTime
		replace duration = duration / (1000*60) // duration in minutes
		gen startDay = date(startDate, "MD20Yhm")
	}
	
	// drop data from programing trials

	drop if session == 1 & startDay ~= 20936
	drop if session == 11 & finished == 0
	drop if session == 12 & finished == 0

	// drop data from participant in session 19 who left in the beginning of the study
	drop if session == 19 & finished == 0
	
	
	
	
	
	forvalues i = 1/18 {
		gen orderAlt`i' = order`i'
		replace changedecision`i' = 0 if changedecision`i' == .
	}
	 

	 
	 
	// all vars indexed by treatment, except for changedecision and tpicture, which are indexed by order displayed. This changes this. 
	reshape long tpicture changedecision orderAlt, i(id) j(treatment)
	drop if orderAlt == .
	drop treatment 
	reshape wide tpicture changedecision, i(id) j(orderAlt)

	drop order19 - order20 
	drop state19 - state20
	
	preserve
		keep id order*
		reshape long order, i(id) j(treatment)
		keep if treatment <= 18
		rename treatment orderCorrect
		reshape wide orderCorrect, i(id) j(order)
		save "data/orderCorrect.dta", replace
	restore
	
	preserve
		keep id orderrisk*
		drop orderriskSum
		egen tag = tag(id)
		keep if tag == 1
		drop tag
		reshape long orderrisk, i(id) j(riskTreatment)
		keep if riskTreatment <= 9
		rename riskTreatment orderriskCorrect
		reshape wide orderriskCorrect, i(id) j(orderrisk)
		save "data/orderriskCorrect.dta", replace
	restore
	
	
	
	

	merge 1:1 id using "data/orderCorrect.dta"

	drop explicit*

	reshape long plik order orderCorrect bet tBeliefs state tpicture tpicQualtrics changedecision ///
		difficulty match nonmatch ///
		, i(id) j(treatment)

	drop order
	rename orderCorrect order
	
	drop if treatment > 18

	
	// code IQ measures 
	if 1 {
		
		// APM Series 1
		gen iq1 = raven37 == 8
		gen iq2 = raven38 == 4
		gen iq3 = raven39 == 5
		gen iq4 = raven40 == 1
		gen iq5 = raven41 == 2
		gen iq6 = raven42 == 5
		gen iq7 = raven43 == 6
		gen iq8 = raven44 == 3
		gen iq9 = raven45 == 7
		gen iq10 = raven46 == 8
		gen iq11 = raven47 == 7
		gen iq12 = raven48 == 6
		
		// APM Series II part 1 of 3

		gen iq13 = raven49 == 5
		gen iq14 = raven50 == 1
		gen iq15 = raven51 == 7
		gen iq16 = raven52 == 4
		gen iq17 = raven53 == 3
		gen iq18 = raven54 == 1
		gen iq19 = raven55 == 6
		gen iq20 = raven56 == 1
		gen iq21 = raven57 == 8
		gen iq22 = raven58 == 4
		gen iq23 = raven59 == 5
		gen iq24 = raven60 == 6

		// APM Series II part 2 of 3
		
		gen iq25 = raven61 == 2
		gen iq26 = raven62 == 1
		gen iq27 = raven63 == 2
		gen iq28 = raven64 == 4
		gen iq29 = raven65 == 6
		gen iq30 = raven66 == 7
		gen iq31 = raven67 == 3
		gen iq32 = raven68 == 8
		gen iq33 = raven69 == 8
		gen iq34 = raven70 == 7
		gen iq35 = raven71 == 6
		gen iq36 = raven72 == 3
		
		// wrong question displayed in session 1
		replace iq17 = raven53 == 2 if session == 1 

		

		gen iq = 0
		forvalues i = 1 / 36 {
			replace iq = iq + iq`i'
		}
	
	
		gen iqInc = session >= 11 & session <= 17
	
			
		replace age = age + 17


		forvalues i = 1 / 18 {
			gen T`i' = treatment == `i'
		}
			

		
		// WTA
		
		preserve
			keep lm*_wta* id
			egen tag = tag(id)
			keep if tag == 1
			drop tag
			reshape long lm1_wta_wta_ lm2_wta_wta_ lm3_wta_wta_ lm4_wta_wta_, i(id) j(line)
			rename lm1_wta_wta_ wta1
			rename lm2_wta_wta_ wta2
			rename lm3_wta_wta_ wta3
			rename lm4_wta_wta_ wta4
			
			gen price = .
			replace price = 0 if line ==  1
			replace price = 0.25 if line ==  2
			replace price = 0.5 if line ==  3
			replace price = 0.75 if line ==  4
			replace price = 1 if line ==  5
			replace price = 1.5 if line ==  6
			replace price = 2 if line ==  7
			replace price = 2.5 if line ==  8
			replace price = 3 if line ==  9
			replace price = 3.5 if line == 10
			replace price = 4 if line == 11
			replace price = 4.5 if line == 12
			replace price = 5 if line == 13
			replace price = 5.5 if line == 14
			replace price = 6 if line == 15
			replace price = 6.5 if line == 16
			replace price = 7 if line == 17
			replace price = 7.5 if line == 18
			replace price = 8 if line == 19
			replace price = 8.5 if line == 20
			replace price = 9 if line == 21
			replace price = 9.5 if line == 22
			replace price = 10 if line == 23
					
			// find switching point
			
			forvalues i = 1 / 4 {
				replace wta`i' = . if wta`i' == 0
				gen price`i' = price * wta`i'
				by id: egen resPrice`i' = min(price`i')
				replace resPrice`i' = 4.50 if resPrice`i' == .
			}
					
			egen tag = tag(id)
			keep if tag == 1
			keep id resPrice*
	
			save "data/resPrices.dta", replace

		restore
	
	}
	
	// Risk preferences
	

		preserve
			keep lm*_riskprefs* id
			egen tag = tag(id)
			keep if tag == 1
			drop tag
			reshape long lm1_riskprefs_riskpref_ lm2_riskprefs_riskpref_ lm3_riskprefs_riskpref_ lm4_riskprefs_riskpref_ lm5_riskprefs_riskpref_ lm6_riskprefs_riskpref_ lm7_riskprefs_riskpref_ lm8_riskprefs_riskpref_ lm9_riskprefs_riskpref_, i(id) j(line)
			rename lm1_riskprefs_riskpref ce1
			rename lm2_riskprefs_riskpref ce2
			rename lm3_riskprefs_riskpref ce3
			rename lm4_riskprefs_riskpref ce4
			rename lm5_riskprefs_riskpref ce5
			rename lm6_riskprefs_riskpref ce6
			rename lm7_riskprefs_riskpref ce7
			rename lm8_riskprefs_riskpref ce8
			rename lm9_riskprefs_riskpref ce9
			
			gen ce = .
			replace ce = -10	if line ==  1
			replace ce = -9		if line ==  2
			replace ce = -8		if line ==  3
			replace ce = -7		if line ==  4
			replace ce = -6		if line ==  5
			replace ce = -5		if line ==  6
			replace ce = -4		if line ==  7
			replace ce = -3		if line ==  8
			replace ce = -2		if line ==  9
			replace ce = -1		if line == 10
			replace ce = 0		if line == 11
			replace ce = 1		if line == 12
			replace ce = 2		if line == 13
			replace ce = 3		if line == 14
			replace ce = 4		if line == 15
			replace ce = 5		if line == 16
			replace ce = 6		if line == 17
			replace ce = 7		if line == 18
			replace ce = 8		if line == 19
			replace ce = 9		if line == 20
			replace ce = 10		if line == 21
			
			
			// find switching point
			
			forvalues i = 1 / 9 {
				replace ce`i' = . if ce`i' == 1
				replace ce`i' = 1 if ce`i' == 0 // 1 if certain amount preferred
				gen resValue`i' = ce * ce`i'
				by id: egen certaintyEq`i' = min(resValue`i')
				replace certaintyEq`i' = 10.50 if certaintyEq`i' == .
			}
					
			egen tag = tag(id)
			keep if tag == 1
			keep id certaintyEq*
			
			forvalues i = 1 / 9 {
				rename certaintyEq`i' ce`i'
				replace ce`i' = ce`i' - 0.5
				replace ce`i' = 10 if ce`i' == . // if subject never selects certain amount, the above algorithm will set ce`i' = .

			}
			

			save "data/certaintyEq.dta", replace

		restore
	


	// Betting decisions and variable renaming

	gen payWin = match
	gen payLose = nonmatch
	
	gen explicit = treatment >= 13
	
	capture drop incentive
	gen incentive = . 
	replace incentive = -1 if payWin == 2
	replace incentive = 0 if payWin == 6
	replace incentive = 1 if payWin == 10

	gen cost = difficulty
	replace difficulty = 99 if explicit == 1
	
	gen round = treatment
	drop treatment
	

	order incentive difficulty explicit 
	sort incentive difficulty explicit 
	egen treatment = group(incentive difficulty explicit)
	order treatment
	
	replace difficulty = . if explicit == 1

	
	gen weight = .
	su treatment
	local numTreatments = r(max)
	forvalues t = 1 / `numTreatments' {
		su state if treatment == `t'
		replace weight = 1 / r(mean)			if state == 1 & treatment == `t'
		replace weight = 1 / (1 - r(mean)) 		if state == 0 & treatment == `t'
	}
	
	
	gen weightIQinc = .
	su treatment
	local numTreatments = r(max)
	forvalues t = 1 / `numTreatments' {
		forvalues i = 0 / 1 {
			su state if treatment == `t' & iqInc == `i'
			replace weightIQinc = 1 / r(mean)			if state == 1 & treatment == `t' & iqInc == `i' 
			replace weightIQinc = 1 / (1 - r(mean)) 	if state == 0 & treatment == `t' & iqInc == `i'
		}
	}
	
	
	gen bayesPost = . 
	replace bayesPost = 0.726 if round == 13 | round == 15 | round == 17
	replace bayesPost = 0.949 if round == 14 | round == 16 | round == 18

	replace bayesPost = 1 - bayesPost if state == 0
	
	egen tagId = tag(id)	
		
	
	drop _merge
	merge n:1 id using "data/resPrices.dta"
	gen monotonicWTA = resPrice1 <= resPrice2 & resPrice2 <= resPrice3 & resPrice3 <= resPrice4

	drop _merge
	merge n:1 id using "data/certaintyEq.dta"	
	
	
	replace male = . if male == -99
	
	label de major 1 "econ" 2 "law" 3 "med" 4 "hum" 5 "sci" 6 "arts"
	label values major major
	
	replace abischnitt = . if abischnitt == 91
	replace abimathe = . if abimathe == 20 | abimathe == 21
	replace abideutsch = . if abideutsch == 20 | abideutsch == 21
	
	replace mathelk = . if mathelk == 3
	replace deutschlk = . if deutschlk == 3
	rename mathelk matheLK
	rename deutschlk deutschLK
	label de yesno 0 "no" 1 "yes"
	label val matheLK yesno
	label val deutschLK yesno

	replace difficulty = difficulty - 2
	
	drop cearray* ceabs* ceword* sequence*
	
	gen tIq = travenapm1_1_pagesubmit + travenapm1_2_pagesubmit + travenamp21_1_pagesubmit + travenamp21_2_pagesubmit + travenamp22_1_pagesubmit + travenamp22_2_pagesubmit
	
	gen tIq1 = travenapm1_1_pagesubmit
	gen tIq2 = travenapm1_2_pagesubmit 
	gen tIq3 = travenamp21_1_pagesubmit 
	gen tIq4 = travenamp21_2_pagesubmit 
	gen tIq5 = travenamp22_1_pagesubmit 
	gen tIq6 = travenamp22_2_pagesubmit

	
	capture drop logTiq*
	forvalues i = 1 / 6 {	
		gen logTiq`i' = log(tIq`i')
	}
	
	capture drop iqSerious
	local threshold = log(60)	
	gen iqSerious =   logTiq1 >= `threshold' ///
					& logTiq2 >= `threshold' ///
					& logTiq3 >= `threshold' ///
					& logTiq4 >= `threshold' ///
					& logTiq5 >= `threshold' ///
					& logTiq6 >= `threshold'
	
		
	
	postfile iqTime id2 iqTimeSlope using "data/iqTimes.dta", replace
	
	capture drop id2
	egen id2 = group(id)
	su id2
	local max = r(max)
	
	forvalues id2 = 1 / `max' {
		preserve
			keep if id2 == `id2'
			keep if tagId == 1
			keep id logTiq*
			reshape long logTiq, i(id) j(block)
			
			reg logTiq block
			local slope = _b[block]
			
			post iqTime (`id2') (`slope') 
		restore
	}	
		
	postclose iqTime
	
	capture drop _merge
	
	merge m:1 id2 using "data/iqTimes.dta"
	
	
	gen ceRiskNeut1 = 0 	
	gen ceRiskNeut2 = 3	
	gen ceRiskNeut3 = 4.8	
	gen ceRiskNeut4 = -4	
	gen ceRiskNeut5 = -1
	gen ceRiskNeut6 = 0.8
	gen ceRiskNeut7 = 4	
	gen ceRiskNeut8 = 7	
	gen ceRiskNeut9 = 8.8	
		
	gen explicitGoodInfo = .
	replace explicitGoodInfo = 1 if round == 14 | round == 16 | round == 18
	replace explicitGoodInfo = 0 if round == 13 | round == 15 | round == 17
		
	gen explicitBadInfo = 1 - explicitGoodInfo
		
	
	
	
	
	
	
	
	save "data/data.dta", replace


	
	

